package com.example.administrator.familfinalcialmanagement.ui.fragment;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.familfinalcialmanagement.R;
import com.example.administrator.familfinalcialmanagement.util.LogUtil;
import com.example.administrator.familfinalcialmanagement.util.dialog.InCommonDialog;

/**
 * Created by Administrator on 2019/3/26.
 */

public abstract class BaseFragment extends Fragment implements View.OnClickListener{

    private View rootView;
    private ProgressDialog mProgressDialog;


    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(rootView==null){
            rootView=inflater.inflate(setLayoutId(), container, false);
        }
        //缓存的rootView需要判断是否已经被加过parent， 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        return rootView;
    }

    public abstract int setLayoutId();

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    public void showToast(int resId){
        if(getActivity()!=null){
            Toast.makeText(getActivity(), resId, Toast.LENGTH_LONG).show();;
        }
    }

    public void showToast(Character text){
        if(getActivity()!=null){
            Toast.makeText(getActivity(),text,Toast.LENGTH_LONG).show();;
        }
    }

    /**
     * showToast
     */
    public void showToast(String str){
        Toast.makeText(getActivity(),str,Toast.LENGTH_LONG).show();
    }
    @Override
    public void onClick(View v) {

    }

    /**
     * 显示加载dialog
     */
    public void showProgressDialog(String msg){
        if(mProgressDialog == null){
            mProgressDialog = new ProgressDialog(getActivity());
            mProgressDialog.setCancelable(true);
        }
        //如果显示的字符为空，则设置默认值
        if(TextUtils.isEmpty(msg)){
            msg = "正在加载数据...";
        }
        mProgressDialog.setMessage(msg);
        mProgressDialog.show();
    }

    /**
     * 显示加载dialog
     */
    public void showProgressDialog(String msg,boolean cancelable){
        if(mProgressDialog == null){
            mProgressDialog = new ProgressDialog(getActivity());
            mProgressDialog.setCancelable(cancelable);
        }
        //如果显示的字符为空，则设置默认值
        if(TextUtils.isEmpty(msg)){
            msg = "正在加载数据...";
        }
        mProgressDialog.setMessage(msg);
        mProgressDialog.show();
    }

    /**
     * 取消加载dialog
     */
    public void cancelProgressDialog(){
        if(mProgressDialog != null){
            mProgressDialog.cancel();
        }
    }

    //设置标题栏中心标题
    public void setTitleText(String str) {
        try {
            TextView text = getActivity().findViewById(R.id.bar_title);
            text.setText(str);
        } catch (Exception e) {
            LogUtil.e("标题栏设置有误");
        }
    }

    //设置标题栏右侧字体（如果是文字）
    public void setRightText(String str) {
        TextView text = getActivity().findViewById(R.id.rightText);
        text.setText(str);
        text.setOnClickListener(this);
    }

    //设置标题栏右侧字体颜色（如果是文字）
    public void setRightTextColor(int id) {
        TextView text = getActivity().findViewById(R.id.rightText);
        text.setTextColor(this.getResources().getColor(id));
    }

    //设置标题栏右侧图片
    public void setRightImg(int id) {
        ImageView image = getActivity().findViewById(R.id.rightImage);
        image.setImageResource(id);
        image.setOnClickListener(this);
    }

    //设置标题栏左侧字体（如果是文字）
    public void setLeftText(String str) {
        getActivity().findViewById(R.id.topBarRelative).setVisibility(View.VISIBLE);
        getActivity().findViewById(R.id.left_icon).setVisibility(View.GONE);
        TextView text = getActivity().findViewById(R.id.leftTitleText);
        text.setText(str);
        text.setOnClickListener(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
        }
    }


}
